package com.igd.socket.sdk.nio.simple_reactor;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class Acceptor implements Runnable {
	
	private ServerSocketChannel serverSocketChannel;
	private Selector selector;
	
	public Acceptor(ServerSocketChannel serverSocketChannel, Selector selector) {
		this.serverSocketChannel = serverSocketChannel;
		this.selector = selector;
	}

	@Override
	public void run() {
		try {
			SocketChannel socketChannel = serverSocketChannel.accept();
			System.out.println("Server client-channal---isConnected：" + socketChannel.isConnected() + " isOpen：" + socketChannel.isOpen() + " isRegistered：" + socketChannel.isRegistered());
			socketChannel.configureBlocking(false);
			//注册读事件 将当前的客户端连接的socket注册进Work中
			SelectionKey readSelectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
			readSelectionKey.attach(new WorkHandler(socketChannel));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
